package com.medicine.service.impl;

import com.medicine.dao.inter.CombatHouseDao;
import com.medicine.exception.BusinessException;
import com.medicine.model.CombatHouse;
import com.medicine.service.ICombatHouseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 输赢分数记录操作类
 * Created by Guyver.com on 2017/10/27 13:24.
 */
@Service
public class CombatHouseServiceImpl implements ICombatHouseService {
    private Logger logger = LoggerFactory.getLogger(CombatHouseServiceImpl.class);
    @Autowired
    private CombatHouseDao combatHouseDao;

    private void checkPrimaryKey(String hid) throws BusinessException {
        if(hid==null || "".equals(hid)) {
            throw new BusinessException("hid：房间号，不允许为空，请检查！");
        }
    }

    public List<CombatHouse> getPlayerCombatHouse(String hid) throws Exception {
        try {
            checkPrimaryKey(hid);
            return this.combatHouseDao.selectByPrimaryKey(hid);
        }catch (Exception e) {
            logger.error("查询房间出错，原因：", e);
            throw e;
        }
    }

    public List<CombatHouse> getPlayerCombatHouseByUserId(String uid) throws Exception {
        try {
            return this.combatHouseDao.selectByUserid(uid);
        }catch (Exception e) {
            logger.error("查询房间出错，原因：", e);
            throw e;
        }
    }

    @Override
    public List<CombatHouse> selectAllByLimit(Map<String, Integer> map) throws Exception {
        try {
            return this.combatHouseDao.selectAllByLimit(map);
        }catch (Exception e) {
            logger.error("查询房间出错，原因：", e);
            throw e;
        }
    }

    @Override
    public List<CombatHouse> selectAll(Map map) throws Exception {
        try {
            return this.combatHouseDao.selectAll(map);
        }catch (Exception e) {
            logger.error("查询房间出错，原因：", e);
            throw e;
        }
    }

    public void addPlayerCombatHouse(CombatHouse combatHouse) throws Exception {
        try {
            combatHouse.setHid(UUID.randomUUID().toString());
            this.combatHouseDao.insert(combatHouse);
        }catch (Exception e) {
            logger.error("新增房间出错，原因：", e);
            throw e;
        }
    }

    public void updatePlayerCombatHouse(CombatHouse combatHouse) throws Exception {
        try {
            checkPrimaryKey(combatHouse.getHid());
            this.combatHouseDao.updateByPrimaryKey(combatHouse);
        }catch (Exception e) {
            logger.error("更新房间出错，原因：", e);
            throw e;
        }
    }
}
